package com.lzw.breezeNovel.webApp.service.impl.user;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.login.LoginUser;
import com.lzw.breezeNovel.common.login.LoginUserHolder;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.model.entity.pojo.user.UserLike;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.mapper.user.UserLikeMapper;
import com.lzw.breezeNovel.webApp.service.user.UserLikeService;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.user
 * @date 2025/3/24
 * @description 用户点赞表
 */

@Service
@SuppressWarnings("all")
public class UserLikeServiceImpl implements UserLikeService {

    /**
     * 注入mapper
     */
    @Autowired
    private UserLikeMapper userLikeMapper;
    /**
     * 注入用户mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<UserLike> list(Integer page, Integer size) {
        //获取当前用户
        Long userId = getUserId();
        //计算分页
        Integer start = getStart(page, size);
        //分页查询
       List<UserLike> userLikeList =  userLikeMapper.selectByUserId(userId, start, size);
       //封装分页对象
       PaginationResultQuery<UserLike> userLikePaginationResultQuery = new PaginationResultQuery<>(0,size,start, userLikeList);
       return userLikePaginationResultQuery;
    }

    /**
     * 删除点赞
     * @param id
     */
    @Override
    public void delete(Long id) {
        //删除点赞
        userLikeMapper.deleteById(id);
    }



    /**
     * 计算分页
     * @param page
     * @param size
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Integer getStart(Integer page, Integer size) {
        page = Optional.ofNullable(page).orElse(1);
        size = Optional.ofNullable(size).orElse(10);
        page = Math.max(page, 1);
        size = Math.max(size, 1);
        return (page - 1) * size;
    }

    /**
     * 获取当前用户
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Long getUserId() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        //获取手机号
        String phone = loginUser.getPhone();
        //根据手机号查询用户信息
        UserInfo userInfo = userInfoMapper.selectByPhone(phone);
        return userInfo.getUserId();
    }
}
